package com.cwt.admin.index;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckOr;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwt.admin.common.BaseController;
import com.cwt.common.result.AjaxResult;
import com.cwt.common.utils.SqlUtils;
import com.cwt.common.vo.PageInfo;
import com.cwt.service.domain.ShopCar;
import com.cwt.service.domain.TbBook;
import com.cwt.service.service.ShopCarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenwt
 * @version V1.0
 * @describe 【】
 * @date 2023年08月02日 16:10:48
 */
@Api(tags = "购物车相关接口", value = "ShopCarController")
@RequestMapping("/shopCar")
@RestController
@SaCheckOr(
        login = @SaCheckLogin,
        role = @SaCheckRole("manage"),
        permission = @SaCheckPermission("order")
)
public class ShopCarController extends BaseController {

    @Autowired
    private ShopCarService shopCarService;

    @ApiOperation(value = "购物车分页", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/page", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody
    AjaxResult shopCarPage(@RequestBody PageInfo info) {
        Page<ShopCar> page = new Page<>(info.getPageNum(), info.getPageSize());
        QueryWrapper<ShopCar> queryWrapper = new QueryWrapper<>();
        SqlUtils.wrapperSetLikeCondition(queryWrapper, info.getParams());
        page = shopCarService.page(page, queryWrapper);
        return AjaxResult.success(page);
    }

    @ApiOperation(value = "购物车添加", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody AjaxResult addShopCar(@RequestBody ShopCar car) {
        return shopCarService.addShopCar(car);
    }

    @ApiOperation(value = "购物车移除", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/remove", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody AjaxResult removeShopCar(@RequestBody ShopCar car) {
        return shopCarService.reduceShopCar(car);
    }

    @ApiOperation(value = "购物车全部移除", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/removeAll", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody AjaxResult removeAllShopCar(@RequestBody ShopCar car) {
        return AjaxResult.success(shopCarService.removeById(car));
    }

    @ApiOperation(value = "购物车总量", protocols = "http", httpMethod = "POST")
    @RequestMapping(value = "/count", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody AjaxResult carCount(@RequestBody long userId) {
        return AjaxResult.success(shopCarService.countByUser(userId));
    }

}
